import 'package:flutter/material.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:fluttertoast/fluttertoast.dart';

//显示list可以左滑删除
class SlidableScreen extends StatefulWidget{
  @override
  SlidableState createState() => SlidableState();
}

class SlidableState extends State<SlidableScreen>{
  GlobalKey key = GlobalKey();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('左滑显现出更多信息'),
        titleSpacing: 50,
        centerTitle: true,
      ),

      body: ListView(
        children: <Widget>[
          Slidable(
            //划出选项卡的动画SlidableBehindActionPane,SlidableScrollActionPane,SlidableDrawerActionPane,SlidableStrechActionPane
            actionPane: SlidableScrollActionPane(),
            //滑动的空间显示的大小比例与item的宽度比  0-1直接
            actionExtentRatio: 0.25,
            child: ListItem(key: key, title: '阻止自动关闭',),
            //左滑右边显示内容
            actions: <Widget>[
              IconSlideAction(
                caption: 'Archive',
                color: Colors.blue,
                icon: Icons.archive,
                closeOnTap: false,
                onTap: (){
                  Fluttertoast.showToast(msg: '点击了Archive');
                },
              ),

              IconSlideAction(
                caption: 'Share',
                color: Colors.lightBlue,
                icon: Icons.share,
                onTap: (){
                  Fluttertoast.showToast(msg: '点击了Share');
                },
              )
            ],
            //右滑，左边显示内容
            secondaryActions: <Widget>[
              IconSlideAction(
                caption: 'Close',
                color: Colors.orange,
                icon:  Icons.close,
                closeOnTap: false,
                onTap: (){
                  Fluttertoast.showToast(msg: '我将在2秒后自动关闭');
                  Future.delayed(Duration(seconds: 2), (){
                    Slidable.of(key.currentContext).close();
                  });
                },
              ),

              IconSlideAction(
                caption: 'Delete',
                color: Colors.red,
                icon:  Icons.delete,
                closeOnTap: false,
                onTap: (){
                  Fluttertoast.showToast(msg: '我将在2秒后删除');
                  Future.delayed(Duration(seconds: 2), (){
                    Navigator.of(key.currentContext).pop(true);
                  });
                },
              ),
            ],
          )
        ],
      ),
    );
  }
}

class ListItem extends StatelessWidget{
  final String title;

  const ListItem({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: (){
        Slidable.of(context).close();
      },
      child: Container(
        color: Colors.white,
        child: ListTile(
          leading: CircleAvatar(
            backgroundColor: Colors.indigoAccent,
            child: Text('A'),
            foregroundColor: Colors.white,
          ),
          title: Text(title),
          subtitle: Text('SlidableDrawerDelegate'),
        ),
      ),
    );
  }

}